using System.Collections.Generic;

public static class TemporalProductManager
{
	private class ProductData
	{
		public string key;

		public int time;

		public string keyShop;

		public ProductData(string _key, int _time, string _keyShop)
		{
			key = _key;
			time = _time;
			keyShop = _keyShop;
		}
	}

	private static string keyCountProduct = "TemporalProductManagerCount";

	private static string keyProductKey = "TemporalProductManagerProductKey_";

	private static string keyProductKeyShop = "TemporalProductManagerProductKeyShop_";

	private static string keyProductTime = "TemporalProductManagerProductTime_";

	private static bool isReadData;

	private static List<ProductData> products;

	public static bool IsAvailable(string keyProduct)
	{
		ReadData();
		return products.Exists((ProductData p) => p.key.Contains(keyProduct));
	}

	public static void RefreshTime(int deltaTime)
	{
		if (TutorialManager.TuturialWorking())
		{
			return;
		}
		ReadData();
		for (int i = 0; i < products.Count; i++)
		{
			products[i].time = products[i].time - deltaTime;
			if (products[i].time < 0)
			{
				TemporalProductEndTimeWindow.NeedShowWindow();
			}
		}
		SaveData();
	}

	public static void AddProduct(string key, int time, string keyShop)
	{
		ReadData();
		products.Add(new ProductData(key, time, keyShop));
		SaveData();
	}

	private static void ReadData()
	{
		if (!isReadData)
		{
			int num = BCWStorage.Get<int>(keyCountProduct);
			products = new List<ProductData>();
			for (int i = 0; i < num; i++)
			{
				string key = BCWStorage.Get<string>(keyProductKey + i);
				int time = BCWStorage.Get<int>(keyProductTime + i);
				string keyShop = BCWStorage.Get<string>(keyProductKeyShop + i);
				products.Add(new ProductData(key, time, keyShop));
			}
			isReadData = true;
		}
	}

	private static void SaveData()
	{
		BCWStorage.Set(keyCountProduct, products.Count);
		for (int i = 0; i < products.Count; i++)
		{
			BCWStorage.Set(keyProductKey + i, products[i].key);
			BCWStorage.Set(keyProductTime + i, products[i].time);
			BCWStorage.Set(keyProductKeyShop + i, products[i].keyShop);
		}
	}

	public static void ClearProduct()
	{
		ReadData();
		products.Clear();
		SaveData();
	}
}
